//
//  ArticleListViewController.m
//  WrittingAnimation
//
//  Created by mac on 2019/4/28.
//  Copyright © 2019 Yang. All rights reserved.
//

#import "ArticleListViewController.h"
#import "ArticleModel.h"
#import "ViewController.h"
#import "ArticleListTableViewCell.h"

@interface ArticleListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation ArticleListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initDataSource];
    [self initTableView];
    
    
    
    NSArray * fontArrays = [[NSArray alloc] initWithArray:[UIFont familyNames]];
    NSLog(@"fontArrays = %@",fontArrays);
    NSLog(@"fontArrays.count = %ld",fontArrays.count);
    NSArray *fontList = [fontArrays sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj1 compare:obj2]; //升序
    }];
    NSLog(@"fontList = %@",fontList);
    NSLog(@"fontList.count = %ld",fontList.count);
    
}

-(void)initDataSource
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ArticleList.plist" ofType:@""];
    NSArray *dataArr = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"dataArr = %@",dataArr);
    [self.dataSource addObjectsFromArray:[ArticleModel mj_objectArrayWithKeyValuesArray:dataArr]];
    [self.tableView reloadData];
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[ArticleListTableViewCell class] forCellReuseIdentifier:@"ArticleListTableViewCell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArticleListTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ArticleModel *model = self.dataSource[indexPath.row];
    cell.titleString = model.ArticleName;
    
    __weak typeof(self) weakSelf = self;
    cell.ActionBlock = ^(BOOL isPortrait) {
        
        [weakSelf enterDrawController:indexPath.row portrait:isPortrait];
        
//        if (isPortrait) {
//            NSLog(@"纵向排版");
//
//        }
//        else
//        {
//            NSLog(@"横向排版");
//        }
    };
    
    return cell;
}

-(void)enterDrawController:(NSInteger)row portrait:(BOOL)isPortrait
{
    ArticleModel *model = self.dataSource[row];
    ViewController *viewController = [[ViewController alloc] init];
    viewController.navigationItem.title = model.ArticleName;
    viewController.model = model;
    viewController.isPortrait = isPortrait;
    [self.navigationController pushViewController:viewController animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    ArticleModel *model = self.dataSource[indexPath.row];
//    ViewController *viewController = [[ViewController alloc] init];
//    viewController.navigationItem.title = model.ArticleName;
//    viewController.model = model;
//    [self.navigationController pushViewController:viewController animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

-(void)dealloc
{
    NSLog(@"销毁文字绘制控制器");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
